<div id="app">
    <admin-page-header>
        <bi-button icon="fa-plus" @click="edit()" type="primary">添加规格</bi-button>
    </admin-page-header>

    <div class="container">
        <div class="row g-2">
            <div class="col-12">
                <bi-card title="搜索">
                    <div class="row g-3">
                        <div class="col-12 col-sm-12 col-lg-12">
                            <bi-input v-model="search.keyword"  placeholder="" icon="fa fa-search" ></bi-input>
                        </div>
                    </div>
                </bi-card>
            </div>
            <div class="col-12">
                <bi-card title="规格列表">
                    <bi-table :data="list"   :loading="loading"  min-height="300" >
                        <bi-table-column label="ID" name="spec_id" width="100" >
                                  
                         </bi-table-column>
                        <bi-table-column label="规格名称" name="title" ></bi-table-column>
                        <bi-table-column label="冷数据" v-slot="{row}" width="100"  >
                            <cold-switch :cold="row.cold" :id="row.spec_id" :url="{controller:'admin.spec',action:'cold'}" :storage="'shop-spec'" ></cold-switch>
                        </bi-table-column>
                        <bi-table-column label="操作" width="220" v-slot="{row}">
                            <bi-button type="primary" @click="$go({action:'item',gets:{spec:row.spec_id}})"  >管理参数</bi-button>
                            <bi-button type="primary" @click="edit(row)"  >编辑</bi-button>
                            <bi-button type="warning" @click="del(row)" >删除</bi-button>
                         </bi-table-column>
                       </bi-table>

                       <template #footer>
                        <bi-pagination v-model="page" :total="total" :count="count" ></bi-pagination>
                    </template>
                </bi-card>


            </div>
                
        </div>
    </div>
</div>

<script>
import editDialog from 'spec/edit-dialog.vue';
import coldSwitch from 'field/cold-switch.vue';
let app = admin.createApp({
    components:{
        coldSwitch,
    },
    data() {
        return {
            loading:false,
            list:[],
            page:1,
            total:0,
            count:0,
            search:{
                keyword:''
            }
        }
    },
    methods: {
        edit(item) {

            let title = item?'编辑规格':'添加规格';

            this.$dialog({
                title,
                type:'vue',
                content:admin.createApp(editDialog,{item}),
                callback:()=>{
                    this.$user.delLocalStorage('shop-spec');
                    this.reFresh();
                }
            });
        },
        del(item){
            this.$confirm('确定要删除规格吗?',()=>{
                this.$ajax({
                    url:this.$url({action:'del'}),
                    data:{
                        spec_id:item.spec_id
                    },
                    success:(res)=>{
                        this.reFresh();
                        this.$user.delLocalStorage('shop-spec');
                        this.$toast({text:'删除成功',type:'success'});
                    }
                })
            })
        },
        reFresh(){
            this.loading = true;
            this.$ajax({
                url:this.$url({action:'list'}),
                data:{
                    ...this.search
                },
                success:(res)=>{
                    this.list = res.list;
                    this.count = res.count;
                    this.loading = false;
                    this.total =  res.total;
                }
            });
        }
    },
    mounted() {
        this.reFresh();
    }
}).mount('#app');
</script>